package com.zdsoft.crm.controller.email;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.mail.MessagingException;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zdsoft.crm.controller.BaseController;
import com.zdsoft.crm.vo.AjaxVo;



@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController {

	/*@Autowired
	JavaMailSender mailSender;*/
	
	@Autowired
	JmsTemplate jmsTemplate;
	
	@Autowired
	BeetlGroupUtilConfiguration beetlConfig;

	@GetMapping
	public ModelAndView index() {
		return new ModelAndView("/email/register.html");
	}

	/*@PostMapping
	@ResponseBody
	public AjaxVo register(String email) throws MessagingException {

		// 生成激活链接
		String validateCodeLink = "http://localhost:8080/crm/register/validate?email=" + email + "&validateCode="
				+ "123";

		GroupTemplate gt = beetlConfig.getGroupTemplate();
		Template t = gt.getTemplate("/common/email/_register.html");
		t.binding("validateCodeLink", validateCodeLink);
		String htmlResult = t.render();
		//System.out.println(htmlResult);

		// 建立邮件消息
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
		// 设置信息
		messageHelper.setTo(email);// 接受email
		messageHelper.setFrom("787683743@qq.com");// 发送email
		messageHelper.setSubject("我是标题"); // 标题
		messageHelper.setText(htmlResult, true);
		mailSender.send(mimeMessage);

		// 调用发注册邮件代码
		return new AjaxVo().setErrorCode("0");
	}*/
	
	@PostMapping
	@ResponseBody
	public AjaxVo register(String email) throws MessagingException {

		// 生成激活链接
		String validateCodeLink = "http://localhost:8080/crm/register/validate?email=" + email + "&validateCode="
				+ "123";
		GroupTemplate gt = beetlConfig.getGroupTemplate();
		Template t = gt.getTemplate("/common/email/_register.html");
		t.binding("validateCodeLink", validateCodeLink);
		String htmlResult = t.render();
		 System.out.println(htmlResult);

		// 调用发注册邮件代码
		jmsTemplate.send(new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage msg = session.createMapMessage();
				msg.setString("email", email);
				msg.setString("htmlResult", htmlResult);
				return msg;
			}
		});
		return new AjaxVo().setErrorCode("0");
	}
}
